package top100.linklist;

import java.util.HashMap;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/21 10:14
 * @description:
 */
public class Solution138 {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        HashMap<Node, Node> map = new HashMap<>();
        Node p = head;
        while (p != null) {
            map.put(p, new Node(p.val));
            p = p.next;
        }
        p = head;
        while (p != null) {
            Node clone = map.get(p);
            clone.next = map.get(p.next);
            clone.random = map.get(p.random);
            p = p.next;
        }
        return map.get(head);
    }

    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
}
